package com.example.ggpicturegg;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.ggpicturegg.Mysql_com.UserDao;
import com.example.ggpicturegg.Mysql_com.Userinfo;
import com.example.ggpicturegg.first.RecyclerviewActivity;
import com.example.ggpicturegg.tool.CommonUtils;

public class Register extends AppCompatActivity {

    private Button btn_register;
    private UserDao userDao;
    private TextView et_uname,et_upass;
    private Handler mainHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        initView();

    }
    private void initView(){
        et_uname = findViewById(R.id.et_uname);
        et_upass = findViewById(R.id.et_upass);
        btn_register = findViewById(R.id.btn_register);

        userDao = new UserDao();
        mainHandler = new Handler(getMainLooper());      //获取主线程

        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doRegister();
                Toast.makeText(Register.this, "注册成功", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(Register.this, Login.class);
                startActivity(intent);
            }
        });

    }

    // 执行注册操作
    private void doRegister(){
        final String uname = et_uname.getText().toString().intern();
        final String upass = et_upass.getText().toString().intern();
        if(TextUtils.isEmpty(uname)){
            CommonUtils.showShortMsg(this,"请输入用户名");
            et_uname.requestFocus();
        }else if(TextUtils.isEmpty(upass)){
            CommonUtils.showShortMsg(this,"请输入用户密码");
            et_upass.requestFocus();
        }else{

            Userinfo item = new Userinfo();
            item.setUname(uname);
            item.setUpass(upass);
            item.getCreatDt(CommonUtils.getDate());
            new Thread(new Runnable() {
                @Override
                public void run() {
                    final int iRow = userDao.addUser(item);
                }
            }).start();
        }


    }
}